﻿#ifndef JZ_CONFIG_TABLE_H_
#define JZ_CONFIG_TABLE_H_

#include <QString>
#include <QDataStream>
#include "JZDbTable.h"

class JZDbConfigTable 
{
public:
    JZDbConfigTable();
    ~JZDbConfigTable();

    QString tableName();
    void setTableName(const QString& tableName);
    const JZDbTable &table();
   
    void addConfig(QString name);
    bool hasConfig(const QString& key);
    void setConfigBuffer(QString key,const QByteArray &buffer);
    QByteArray getConfigBuffer(QString key);

    template<class T>
    T getConfig(QString key)
    {
        QByteArray buffer = getConfigBuffer(key);
        if (buffer.isEmpty())
            return T();

        int magic = 0;
        QDataStream s(&buffer, QIODevice::ReadOnly);
        s >> magic;
        if (magic != sizeof(T))
            return T();

        T t;
        s >> t;
        return t;
    }

    template<class T>
    void setConfig(QString key,const T &t)
    {
        QByteArray buffer;
        QDataStream s(&buffer, QIODevice::WriteOnly);        
        
        int magic = sizeof(t);
        s << magic;
        s << t;

        setConfigBuffer(key, buffer);        
    }

protected:
    JZDbTable m_table;
};

#endif